﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using EventMgrLib;
using IOCtrls;
using SmartMotion.Core;

namespace SmartMotion.AllPages.Debug
{
    public partial class FrmIODebug : UIPage
    {
        private IOMgr _ioMgr;
        private EventMgr _em;
        public FrmIODebug()
        {
            InitializeComponent();
        }

        private void FrmIODebug_Resize(object sender, EventArgs e)
        {
            panInput.Width = (Width - 5) / 2;
        }

        private void FrmIODebug_Load(object sender, EventArgs e)
        {
            _ioMgr=IOMgr.Ins;
            _em=EventMgr.Ins;
            _em.GetEvent<IOConfigChanged>().Subscribe(IoChanged);
            IoChanged();
        }

        private void IoChanged()
        {
            flowPanInput.Controls.Clear();
            flowPanOutput.Controls.Clear();
            foreach (var ioMgrCard in _ioMgr.Cards)
            {
                //Load Di
                foreach (var ioIn in ioMgrCard.DIInuse)
                {
                    InputCtr io = new InputCtr();
                    io.Tag = ioIn;
                    io.State = ioIn.Value;
                    io.LabelDoubleClick += Io_DoubleClick;
                    io.Text = string.IsNullOrEmpty(ioIn.Alias) ? ioIn.Name : ioIn.Alias;
                    flowPanInput.Controls.Add(io);
                }
                //Load Do
                foreach (var ioIn in ioMgrCard.DOInuse)
                {
                    OutputCtr io = new OutputCtr();
                    io.Tag = ioIn;
                    io.State = ioIn.Value;
                    io.Text = string.IsNullOrEmpty(ioIn.Alias) ? ioIn.Name : ioIn.Alias;
                    io.LabelDoubleClick += Io_DoubleClick;
                    io.StateChanged += Io_StateChanged;
                    flowPanOutput.Controls.Add(io);
                }
            }
        }
            
        private void Io_DoubleClick(object sender)
        {
            if (sender is InputCtr input)
            {
                if (input.Tag is IOIn ioIn)
                {
                    string newName = null;
                    if (UIInputDialog.InputStringDialog(ref newName, true, "请输入新的IO名称"))
                    {
                        ioIn.Alias = newName;
                        input.Text = newName;
                        _ioMgr.Save();
                    }

                }
            }

            if (sender is OutputCtr output)
            {
                if (output.Tag is IOOut ioOut)
                {
                    string newName = null;
                    if (UIInputDialog.InputStringDialog(ref newName, true, "请输入新的IO名称"))
                    {
                        ioOut.Alias = newName;
                        output.Text = newName;
                        _ioMgr.Save();
                    }
                }
            }

        }

        private void Io_StateChanged(object io, bool arg2)
        {
            if (io is IOOut  ioout)
            {
                //ioout.SetValue(arg2);
            }
        }

        private void FrmIODebug_FormClosing(object sender, FormClosingEventArgs e)
        {
            _em.GetEvent<IOConfigChanged>().Unsubscribe(IoChanged);
        }
    }   
}   
